<?php

namespace app\api\model;

use fast\Http;
use think\Model;
use think\Request;

Class Order extends Model {


    public function __construct($merchant_id = '', $key = '') {
        $this->domain  = Request::instance()->domain();
    }

    public function createOrder($params)
    {

        $data = [
            'ordersn'         => $params['ordersn'],
            'price'         => $params['price'],
            'time'          => date('Y-m-d H:i:s'),
            'paytype'       => $params['paytype'],
            'notify_status' => 0,
            'status'        => '待付款',
            'openid'        => $params['openid'],
            'callbackurl'   => $params['callbackurl'],
            'nonce_str'     => $params['nonce_str'],
            'title'         => $params['title'],
            'payplatform'   => $params['payplatform'],
            'platform'      => $params['platform'],
            'appid'         => $params['appid'],
            'merchant_no'   => $params['merchant_no'],
            'verifycode'    => $params['verifycode'],
            'platform_name' => $params['platform_name'],
            'ip'            => $params['ip'],
            'goodstitle'    => $params['goodstitle'],
            'platform_lists_id'    => $params['platform_lists_id'],
            'pay_code_lists_id'    => $params['pay_code_lists_id'],

        ];


        Db('orderLists')->insert($data);
        return Db('orderLists')->getLastInsID();
    }

    public function createOrderPost($params)
    {

        $orderpostid          = $params['orderpostid'] ?? '';
        $ordersn              = $params['ordersn'] ?? '';
        $openid              = $params['openid'] ?? '';
        unset($params['orderpostid']);

        if (!$orderpostid){
            //查看是否已经插入

//            $orderPost = Db('orderPost')->where(['ordersn' => $ordersn])->find();
//
//            if ($orderPost){
////                Db('orderPost')->where(['ordersn' => $ordersn])->update(['openid'=>$openid]);
//                return $orderPost['id'];
//            }

            Db('orderPost')->insert($params);
            return Db('orderPost')->getLastInsID();
        }

        return $orderpostid;

    }
}

